/**
 * @Author：ming
 * @Date：2023/9/26 11:09 AM
 * @Filename：Lc20
 */

import java.util.Stack;

/**
 * @author ming
 */
public class Lc20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] cc = s.toCharArray();
        for (char c : cc) {
          if(c==')'){
              if(stack.isEmpty()){
                  return false;
              }
              Character peek = stack.peek();
              if(peek=='('){
                  stack.pop();
              }else {
                  return false;
              }
          }else if(c=='}'){
              if(stack.isEmpty()){
                  return false;
              }
              Character peek = stack.peek();
              if(peek=='{'){
                  stack.pop();
              }else {
                  return false;
              }
          }else if(c==']'){
              if(stack.isEmpty()){
                  return false;
              }
              Character peek = stack.peek();
              if(peek=='['){
                  stack.pop();
              }else {
                  return false;
              }
          }else {
              stack.push(c);
          }
        }
        boolean flag = false;
        if(stack.isEmpty()){
            flag = true;
        }else {
            flag = false;
        }

        return flag;
    }
}
